﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.System;
//modified by jeff.yuan 增加通过URL传查询条件，传客户和日期
public partial class Message_List : PageBase
{
    private const string NEW_PAGE = "Customer_Select.aspx";
    private OrgainzationManager orgManager = new OrgainzationManager();
    MessageManager messagemanager = new MessageManager();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //初始化日期
            txtStart.Text = DateTime.Now.ToString("yyyy-MM-dd");
            txtEnd.Text = DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd");
            DataBind();
            if (txtHidOrg.Value == "")
            {
                this.txtHidOrg.Value = OrgHidValue;
                var data = new OrgainzationManager().SearchOrgTree(MyStateManager.DomainID, MyStateManager.OrgID, null);

            }
            InitForm();
            BindGrid();

        }
    }
   
    
    private void InitForm()
    {
        DictionaryItemsManager dim = new DictionaryItemsManager();
        //销售渠道
        ddlMessageLevel.DataTextField = "Name";
        ddlMessageLevel.DataValueField = "DictionaryItemId";
        ddlMessageLevel.DataSource = dim.GetItemList(DictionaryEnum.MessageType, this.MyStateManager.DomainID);
        ddlMessageLevel.DataBind();
        ddlMessageLevel.Items.Insert(0, new ListItem("-所有-", ""));
    }

    private void BindGrid()
    {

       
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

      
        if (ddlMessageLevel.SelectedValue != string.Empty)
        {
            sb.Append(" And  CONTENT_LEVEL = '" + ddlMessageLevel.SelectedValue + "' ");
        }
       
        if (txtCode.Text != string.Empty)
        {
            sb.Append(" AND (TOPIC LIKE '%" + txtCode.Text.Trim() + "%' or  CONTENT LIKE '%" + txtCode.Text.Trim() + "%')");
        }

        if (txtStart.Text != string.Empty)
        {
            sb.Append(" AND BEGIN_DATE>=" + txtStart.Text.Replace("-", "").Replace("/", ""));
        }
        if (txtEnd.Text != string.Empty)
        {
            sb.Append(" AND END_DATE<=" + txtEnd.Text.Replace("-", "").Replace("/", ""));
        }
        if (ddlActive.SelectedValue != string.Empty)
        {
            sb.Append(" AND  ACTIVE='" + ddlActive.SelectedValue + "'  ");
        }
        
        MessageManager mm = new MessageManager();
        Jumpton.Persistent.ObjectTable dt = mm.GetReceiveMessageList(MyStateManager.DomainID,sb.ToString(),MyStateManager.PersonID.ToString());
        if (dt != null)
        {
            GridPager1.DataSource = dt;
            GridPager1.GridView = gvMessage;
            GridPager1.BindData();
            GridPager1.Visible = gvMessage.Rows.Count > 0;

        }

    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    public void GridViewToolbarUserControl1_OnGVTBSetColumnsButtonEvent(object sender, EventArgs e)
    {
        BindGrid();
    }

   

    protected  void   Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int rowCount = gvMessage.Columns.Count;

            //TODO:
            e.Row.Cells[1].Text = e.Row.Cells[1].Text == "81" ? "工作日志" : "紧急汇报";
            e.Row.Cells[2].Text = DateTime.ParseExact(e.Row.Cells[2].Text, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture
                      , System.Globalization.DateTimeStyles.None).ToString("yyyy-MM-dd");
            e.Row.Cells[3].Text = DateTime.ParseExact(e.Row.Cells[3].Text, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture
                    , System.Globalization.DateTimeStyles.None).ToString("yyyy-MM-dd");
        }
    }

    
}
